<?php

namespace Home\Controller;

use Think\Controller;

class gerendangController extends VController
{
    //展示日志列表
    public function index()
    {
        $uid = $_SESSION['qzone']['id'];
        $list = M('message')->where($uid)->select();
        $this->assign('list', $list);
        $this->display();
    }


    public function gerendang()
    {

        $data = M('user')->find($this->userData['id']);
        $this->assign('data', $data);
        $photoNum = M('AlbumPhoto')->where("uid='{$this->userData['id']}'")->count();
        $photoNum += M('SayPhoto')->where("uid='{$this->userData['id']}'")->count();
        $sayNum = M('Say')->where("uid='{$this->userData['id']}'")->count();
        $logNum = M('Log')->where("uid='{$this->userData['id']}'")->count();
        $this->assign('myUserData', $this->myUserData);
        $this->assign('userData', $this->userData);
        $this->assign('isMy', $this->isMy);
        $this->assign('photoNum', $photoNum);
        $this->assign('sayNum', $sayNum);
        $this->assign('logNum', $logNum);
        $this->display();
    }

    public function updateGerendang()
    {
        $data = I('post.');
        $data['id'] = $this->myUserData['id'];
        $model = M('User');
        $model->create($data);
        $result = $model->save();
        if ($result > 0) {
            $this->ajaxReturn(array('status' => 0, 'message' => '编辑成功！'));
        }
        $this->ajaxReturn(array('status' => 1, 'message' => '编辑失败！'));
    }

    public function updatePasswd()
    {
        if(IS_AJAX)
        {
            $data['passwd'] = md5(I('post.passwd'));
            if(I('post.passwd')!= I('post.repasswd'))
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '再次密码输入不一致！'));
            }
            $model = M('User');
            if(md5(I('post.oldpasswd')) != $model->field('passwd')->find($this->myUserData['id'])['passwd'])
            {
                $this->ajaxReturn(array('status' => 1, 'message' => '您的旧密码不正确！'));
            }
            $result = $model->where("id='{$this->myUserData['id']}'")->save($data);
            if($result>0)
            {
                $this->ajaxReturn(array('status' => 0, 'message' => '密码已修改，即将退出，请重新登录！'));
            }
            $this->ajaxReturn(array('status' => 1, 'message' => '密码修改失败！'));
        }else
        {
            $this->ajaxReturn(array('status' => 1, 'message' => '非法来源！'));
        }
    }


    public function Update()
    {
        if (empty($_POST)) {
            $this->error('请完善数据!');
            exit;
        }
        $user = M('User');
        $user->create();
        $user->id = $_SESSION['qzone']['id'];
        if ($user->save() > 0) {
            $this->success('编辑成功', U('gerendang'));
        } else {
            $this->error('编辑失败');
        }
    }

    public function edit()
    {
        //接收参数
        $id = I('get.id/d');
        //查数据
        $data = M('user')->find($id);

        $this->assign('title', '编辑用户');
        $this->assign('data', $data);
        $this->display('Gerendang/edit');
    }

    public function showzone()
    {

        $this->display();
    }


    public function xiugaimima()
    {

        $ops = md5($_POST['passwd']);
        $nps = md5($_POST['newpasswd']);
        $user = M('user');
        $userData = $user->where("id={$_SESSION['qzone']['id']}")->find();
        if ($userData['passwd'] == $ops) {
            $data = array('passwd' => $nps);
            $user->where("id='{$_SESSION['qzone']['id']}'")->setField($data);
            $this->ajaxReturn(array('status' => 0));

        } else {
            $this->ajaxReturn(array('status' => 1));

        }


    }


    public function gerendangUpload()
    {
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->savePath = '../Public/uploads/'; // 设置附件上传根目录
        $upload->subName = array('date', 'Ymd'); // 设置附件上传（子）目录
        // 上传文件
        $info = $upload->upload();
        if (!$info) {// 上传错误提示错误信息

            $this->ajaxReturn(array('status' => 1));
        } else {// 上传成功
            $tmp = substr($info['Filedata']['savepath'], 9);
            $path = $tmp . $info['Filedata']['savename'];
            //将图片插入数据库
            $user = M('User');
            $data = array('zonedp' => $path);
            $result = $user->where("id='{$_SESSION['qzone']['home']['id']}'")->setField($data);
            $this->ajaxReturn(array('status' => 0, 'path' => $path, 'result' => $result, 'xsh' => $_SESSION['qzone']['home']['id']));

        }
    }


}
   